// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoWorld;
import zombie.iso.areas.NonPvpZone;
import zombie.util.StringUtils;

public class SyncNonPvpZonePacket implements INetworkPacket {
    public final NonPvpZone zone = new NonPvpZone();
    public boolean doRemove;

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.zone.load(b, IsoWorld.getWorldVersion());
        this.doRemove = b.get() == 1;
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.zone.save(b.bb);
        b.putBoolean(this.doRemove);
    }

    @Override
    public boolean isConsistent() {
        return !StringUtils.isNullOrEmpty(this.zone.getTitle());
    }

    @Override
    public String getDescription() {
        return String.format(
            "\"%s\" remove=%b size=%d (%d;%d) (%d;%d)",
            this.zone.getTitle(),
            this.doRemove,
            this.zone.getSize(),
            this.zone.getX(),
            this.zone.getY(),
            this.zone.getX2(),
            this.zone.getY2()
        );
    }

    public void process() {
        if (this.doRemove) {
            NonPvpZone.getAllZones().removeIf(nonPvpZone -> nonPvpZone.getTitle().equals(this.zone.getTitle()));
        } else if (NonPvpZone.getZoneByTitle(this.zone.getTitle()) == null) {
            NonPvpZone.getAllZones().add(this.zone);
        }
    }
}
